﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Common;
using Interface;
using Entities;
using System.ServiceModel;
using System.Runtime.InteropServices;
using System.Collections;
using System.ServiceModel.Channels;
using System.Xml;
using System.ServiceModel.Description;

namespace GUI.DangNhap
{
    public partial class frmDangNhap : Form
    {
        #region Delegate
        //public void GetValue(Entities.TaiKhoan taikhoan)
        //{
        //    this.TaiKhoanDangNhap = taikhoan;
        //    switch (taikhoan.HanhDong)
        //    {
        //        case "Thoat":
        //            Application.Exit();
        //            break;
        //        case "DangNhap":
        //            if (this.DangNhap() == true)
        //            {
        //                frmTrangChinh frmMain = new frmTrangChinh();
        //                this.Hide();
        //                frmMain.Show();
        //            }
        //            else
        //            {
        //                GUI.DangNhap.frmXuLy frm = new GUI.DangNhap.frmXuLy();
        //                frm.GetData = new GUI.DangNhap.frmXuLy.GetBoolean(GetValue);
        //                frm.MdiParent = this;
        //                frm.Show();
        //            }
        //            break;
        //        default:
        //            break;
        //    }
        //}
        #endregion

        #region New Object
        private Entities.TaiKhoan TaiKhoanDangNhap = null;
        public frmDangNhap()
        {
            InitializeComponent();
            this.TaiKhoanDangNhap = new TaiKhoan();
        }
        #endregion

        #region Connect Service
        public string GetConfig(string keys)
        {
            string url = string.Empty;
            try
            {
                System.Reflection.Assembly getfile = System.Reflection.Assembly.GetExecutingAssembly();
                ArrayList arr = new ArrayList();
                XmlDocument xdoc = new System.Xml.XmlDocument();
                xdoc.Load(getfile.Location + ".config");
                foreach (XmlNode xNode in xdoc["configuration"]["appSettings"])
                {
                    if (xNode.Name == "add")
                    {

                        if (xNode.Attributes.GetNamedItem("key").Value.ToUpper() == keys.ToUpper())
                        {
                            url = xNode.Attributes.GetNamedItem("value").Value.ToString();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                url = null;
            }
            return url;
        }
        public Boolean Start()
        {
            try
            {
                string url = GetConfig("Http");
                if (url == null)
                {
                    MessageBox.Show("App.config không timg thấy đường dẫn tới máy chủ");
                    return false;
                }
                EndpointAddress endpoint = new EndpointAddress(url);
                BasicHttpBinding bind = new BasicHttpBinding();
                bind.MaxBufferPoolSize = (int)67108864;
                bind.MaxBufferSize = 67108864;
                bind.MaxReceivedMessageSize = (int)67108864;
                bind.TransferMode = TransferMode.Buffered;
                bind.ReceiveTimeout = new TimeSpan(20, 0, 0);
                bind.Security.Mode = BasicHttpSecurityMode.None;
                bind.CreateBindingElements();
                bind.HostNameComparisonMode = HostNameComparisonMode.Exact;
                bind.TransferMode = TransferMode.Streamed;
                ChannelFactory<Interface.IDataObject> objCF = new ChannelFactory<Interface.IDataObject>(bind, endpoint);
                Common.StaticVariable.InterfaceObject = objCF.CreateChannel();
                Entitystatic.date = Common.StaticVariable.InterfaceObject.SelectDate();
                return true;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox.Show("Kết nối service thất bại. Kiểm tra service !");
                Application.Exit();
                return false;
            }
        }
        #endregion

        #region Login
        private Boolean DangNhap()
        {
            Boolean kt = false;
            try 
	        {	       
                if(this.TaiKhoanDangNhap.TenDangNhap == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                if (this.TaiKhoanDangNhap.MatKhauDangNhap == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                Entities.TaiKhoan tk= Common.StaticVariable.InterfaceObject.DangNhap(TaiKhoanDangNhap.TenDangNhap, TaiKhoanDangNhap.MatKhauDangNhap);
                if (tk == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                Common.StaticVariable.User = tk;
                Common.Utilities.User = tk;
                UtilitiesVariable.User = tk;
                DataTable dt = Common.StaticVariable.InterfaceObject.SelectChiTietQuyen(tk.TenNhomQuyen);
                if (dt != null)
                {
                    var ar = from a in dt.AsEnumerable()
                             select new Entities.ChiTietQuyen
                                 {
                                     TenNhomQuyen = a.Field<string>("TenNhomQuyen"),
                                     TenForm = a.Field<string>("TenForm"),
                                     Ten = a.Field<string>("Ten"),
                                     QuyenSua = a.Field<bool>("QuyenSua"),
                                     QuyenXem = a.Field<bool>("QuyenXem"),
                                     QuyenThem = a.Field<bool>("QuyenThem"),
                                     QuyenXoa = a.Field<bool>("QuyenXoa")
                                 };
                    Common.StaticVariable.ACTQ = ar.ToArray<Entities.ChiTietQuyen>();
                    Common.Utilities.chitiet = ar.ToArray<Entities.ChiTietQuyen>();
                    UtilitiesVariable.chitiet = Common.Utilities.chitiet;
                }
                else
                {
                    Common.StaticVariable.ACTQ = new ChiTietQuyen[0];
                    Common.Utilities.chitiet = new ChiTietQuyen[0];
                    UtilitiesVariable.chitiet = Common.Utilities.chitiet;
                }
		        kt = true;
                this.TaiKhoanDangNhap = null;
	        }
	        catch (Exception ex)
	        {
                string s = ex.Message;
		        kt= false;
	        }
            return kt;
        }
        #endregion

        #region Load
        //private void frmDangNhap_Load(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Start();
        //        GUI.DangNhap.frmXuLy frm = new GUI.DangNhap.frmXuLy();
        //        frm.GetData = new GUI.DangNhap.frmXuLy.GetBoolean(GetValue);
        //        frm.MdiParent = this;
        //        frm.Show();
        //    }
        //    catch (Exception ex)
        //    {
        //        string s = ex.Message;
        //    }

        //}
        #endregion
    }
}
#endregion
